package sg.edu.nus.iss.billing.parse;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
/**
 * @author sammil
 *
 */
public class AccountHeaderParser extends Parser {
	
	public static final String PAGE_TRANS_TITLE = "ONE# TRANSACTION DATA";
	public static final String PAGE_PAYMENT_TITLE = "ONE# PAYMENT HISTORY";
	private static final String COMPANY_NAME_END_LINE = "======================";
	private static final String ACCONT_NO_LABEL = "+ACCOUNT NO:";
	private static final int ACCONT_NO_LENGTH = 15;
	
	private static final int MAX_LENGTH = 3 * Parser.MAX_LINE_LEN;
	
	static String accountNo = null; 
	
	private AccountHeaderParser() {
	}

	public static final AccountHeaderParser SINGLETON = new AccountHeaderParser();
	
	@Override
	public <L> boolean parse(LineNumberReader in, HashMap<String,ArrayList<L>> outputMap)
			throws IOException {
		// TODO Auto-generated method stub
		try {

			// remember where we started from
			in.mark(MAX_LENGTH);
			//if (!consume(in, PAGE_TRANS_TITLE, PAGE_PAYMENT_TITLE)) {
			if (!consume(in, PAGE_TRANS_TITLE) && !consume(in,  PAGE_PAYMENT_TITLE)) {
				return false;
			}
			if (!consume(in, COMPANY_NAME_END_LINE)) {
				return false;
			}
			StringReader line = nextLine(in);

			// Read the first line
			accountNo = readToken(ACCONT_NO_LABEL, line, ACCONT_NO_LENGTH).trim();
			System.out.println("accountNo[" +  accountNo+"]");
			outputMap.put(accountNo,new ArrayList<L>() );
			//outputMap.put(accountNo,callTransList );
			
			
		} catch (IOException e) {

			System.err.println("Problem in AccountHeaderParser :" +  e);
			// go back to where we started
			in.reset();
			// re-throw the exception
			throw e;
		}
		System.out.println("Parsed  AccountHeader record");
		return true;
	}

}
